<?php

namespace App\Console\Commands;

use App\Model\Branch;
use App\Model\Order;
use App\Model\PushOrder;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Matrix\Exception;

class ryPush extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'ryPush';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        while(true){
            try {
                //控制是否推单给下线
                $branch = Branch::where("id", 12)->first();
                if ($branch['tel'] == 1) {
                    $order = DB::select("select id,buytime,chargeaccount,remainingnumber,price,num,orderno from tiktok_pushorder where status = '未处理' order by id asc limit 1");
//                $order = DB::select("select id,buytime,chargeaccount,remainingnumber,num,orderno from tiktok_order1 where status = '未处理'   order by id asc limit 1");
                    $num = Branch::where("tel", 1)->count();
                    if (isset($order[0])) {
                        $order1 = $order[0];
                        $order = (array)$order1;
                        if ($order['id'] % $num == 0) {
                            $res = $this->ry_PushOrder($order['chargeaccount'], $order['orderno'], $order['remainingnumber']);
                            $info['ext'] = $res;
                            PushOrder::where("orderno", $order['orderno'])->update($info);
//                        Order1::where("orderno",$order['orderno'])->update($info);
                            $res = json_decode($res);
                            if ($res->status == 200) {
                                $info['status'] = '已处理';
                                $info['branch'] = '已推送,等待反馈';
                                $info['isok'] = 3;
                                $info['bid'] = 12;
                                $info['time'] = time();
                                PushOrder::where('orderno', $order['orderno'])->update($info);
                                Order::where('orderno', $order['orderno'])->update($info);
//                            Order1::where('orderno', $order['orderno'])->update($info);
                                $branch = Branch::where("id", 12)->first();
                                $orderInfo['num'] = $branch['num'] + 1;
                                $orderInfo['money'] = $branch['money'] + $order['remainingnumber'];
                                Branch::where("id", $branch['id'])->update($orderInfo);
                            }
                        }
                    }
                }
            } catch (\Exception $e) {
                echo $e->getMessage();
            }
            sleep(2);
        }
    }

    //软游   推送方法
    public function ry_PushOrder($dyid, $orderid, $money, $num = 1)
    {
        $count = [50, 100, 200, 300, 500, 1000];
        if (in_array($money, $count)) {
            switch ($money) {
                case 50:
                    $goodsCode = 'goods_201';
                    break;
                case 100:
                    $goodsCode = 'goods_24';
                    break;
                case 200:
                    $goodsCode = 'goods_25';
                    break;
                case 300:
                    $goodsCode = 'goods_204';
                    break;
                case 500:
                    $goodsCode = 'goods_205';
                    break;

            }
            $time = time() . rand(100, 999);
            $callBackUrl = 'http://112.124.24.221/admin/callback/ry_AutoConfirmOrder';
            $sign = md5('zmypcennn9businessCode=business_21businessOrderId=' . $orderid . 'callBackUrl=' . $callBackUrl . 'chargeCode=' . $dyid . 'goodsCode=' . $goodsCode . 'num=' . $num . 'timeStamp=' . $time . 'tradeIp=127.0.0.1zmypcennn9');

            $post = [
                'businessCode' => 'business_21',
                'businessOrderId' => $orderid,
                'callBackUrl' => $callBackUrl,
                'chargeCode' => $dyid,
                'goodsCode' => $goodsCode,
                'num' => 1,
                'sign' => $sign,
                'timeStamp' => $time,
                'tradeIp' => '127.0.0.1'
            ];
            $json_post = json_encode($post);
            return $this->ry_curl_post_https('http://api.rytjsj.com:60000/business-center/order/sendOrder', $json_post, $orderid);
        }

    }

    function ry_curl_post_https($url, $data, $orderid)
    { // 模拟提交数据函数
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
//        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        $tmpInfo = curl_exec($curl); // 执行操作
        if (curl_errno($curl)) {
            echo 'Errno' . curl_error($curl);//捕抓异常
        }
        curl_close($curl); // 关闭CURL会话
        return $tmpInfo; // 返回数据，json格式
    }
}
